package com.wjc.basemodule.utils

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.FileInputStream
import java.io.InputStream
import java.lang.Exception
import kotlin.math.roundToInt
import android.content.res.AssetManager
import java.io.IOException
import java.lang.Deprecated


/**
 * ClassName: com.wjc.basemodule
 * Description: Bitmap 操作
 * JcChen on 2021.08.15.08:18
 */
object BitmapUtil {


    /*** 获取缩放后的本地图片
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @return
     * readBitmapFromFileDescriptor 替代
     */
    @Deprecated
    fun readBitmapFromFile(filePath: String, width: Int, height: Int): Bitmap? {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(filePath, options)
        val srcWidth = options.outWidth.toFloat()
        val srcHeight = options.outHeight.toFloat()
        var inSampleSize = 1
        if (srcHeight > height || srcWidth > width) {
            inSampleSize = if (srcWidth > srcHeight) {// width 大
                (srcHeight / height).roundToInt()// 最接近的整数
            } else {
                (srcWidth / width).roundToInt()
            }
        }
        options.inJustDecodeBounds = false
        options.inSampleSize = inSampleSize
        return BitmapFactory.decodeFile(filePath, options)
    }

    /***
     *  对比 readBitmapFromFile 高效
     * 获取缩放后的本地图片
     *  @param filePath 文件路径
     *  @param width 宽
     *  @param height 高
     *  @return
     */
    fun readBitmapFromFileDescriptor(filePath: String, width: Int, height: Int): Bitmap? {
        try {
            val fis = FileInputStream(filePath)
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeFileDescriptor(fis.fd, null, options)
            val srcWidth = options.outWidth.toFloat()
            val srcHeight = options.outHeight.toFloat()
            var inSampleSize = 1
            if (srcHeight > height || srcWidth > width) {
                inSampleSize = if (srcWidth > srcHeight) {
                    (srcHeight / height).roundToInt()
                } else {
                    (srcWidth / width).roundToInt()
                }
            }
            options.inJustDecodeBounds = false
            options.inSampleSize = inSampleSize
            return BitmapFactory.decodeFileDescriptor(fis.fd, null, options)
        } catch (ex: Exception) {
        }
        return null
    }

    /*** 获取缩放后的本地图片
     * @param ins 输入流
     * @param width 宽
     * @param height 高
     * @return
     */
    fun readBitmapFromInputStream(ins: InputStream, width: Int, height: Int): Bitmap? {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeStream(ins, null, options)
        val srcWidth = options.outWidth.toFloat()
        val srcHeight = options.outHeight.toFloat()
        var inSampleSize = 1
        if (srcHeight > height || srcWidth > width) {
            inSampleSize = if (srcWidth > srcHeight) {
                (srcHeight / height).roundToInt()
            } else {
                (srcWidth / width).roundToInt()
            }
        }
        options.inJustDecodeBounds = false
        options.inSampleSize = inSampleSize
        return BitmapFactory.decodeStream(ins, null, options)
    }

    /*
     Res资源加载方式
    此种方式相当的耗费内存 建议采用 decodeStream 代替 decodeResource
    BitmapFactory.decodeResource 加载的图片可能会经过缩放，该缩放目前是放在 java 层做的，效率
    比较低，而且需要消耗 java 层的内存。因此，如果大量使用该接口加载图片，容易导致OOM错误
    BitmapFactory.decodeStream 不会对所加载的图片进行缩放，相比之下占用内存少，效率更高。
    这两个接口各有用处，如果对性能要求较高，则应该使用 decodeStream ；如果对性能要求不高，且需
    要 Android 自带的图片自适应缩放功能，则可以使用 decodeResource 。
    */
    fun readBitmapFromResource(
        resources: Resources, resourcesId: Int, width: Int, height: Int
    ): Bitmap? {
        val ins = resources.openRawResource(resourcesId)
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeStream(ins, null, options)
        val srcWidth = options.outWidth.toFloat()
        val srcHeight = options.outHeight.toFloat()
        var inSampleSize = 1
        if (srcHeight > height || srcWidth > width) {
            inSampleSize = if (srcWidth > srcHeight) {
                (srcHeight / height).roundToInt()
            } else {
                (srcWidth / width).roundToInt()
            }
        }
        options.inJustDecodeBounds = false
        options.inSampleSize = inSampleSize
        return BitmapFactory.decodeStream(ins, null, options)
    }

    /***
     * Assets资源加载方式
     *  @param filePath 文件路径,即文件名称
     *  @return
     *  */
    fun readBitmapFromAssetsFile(context: Context, filePath: String): Bitmap? {
        var image: Bitmap? = null
        val assetManager: AssetManager = context.resources.assets
        try {
            val fileInputString = assetManager.open(filePath)
            image = BitmapFactory.decodeStream(fileInputString)
            fileInputString.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return image
    }

    // 从二进制数据读取图片
    fun readBitmapFromByteArray(data: ByteArray, width: Int, height: Int): Bitmap? {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeByteArray(data, 0, data.size, options)
        val srcWidth = options.outWidth.toFloat()
        val srcHeight = options.outHeight.toFloat()
        var inSampleSize = 1
        if (srcHeight > height || srcWidth > width) {
            inSampleSize = if (srcWidth > srcHeight) {
                (srcHeight / height).roundToInt()
            } else {
                (srcWidth / width).roundToInt()
            }
        }
        options.inJustDecodeBounds = false
        options.inSampleSize = inSampleSize
        return BitmapFactory.decodeByteArray(data, 0, data.size, options)
    }



}